package com.sky.controller.user;

import com.sky.result.Result;
import com.sky.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通用接口
 */
@RestController("userCommonController")
@RequestMapping("/user/common")
@Api(tags = "C端-通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 文件下载
     * @param name
     * @return
     */
    @GetMapping("/download")
    @ApiOperation("文件下载")
    public Result<String> download(@RequestParam("name") String name) {
        log.info("文件下载请求，name：{}", name);
        
        // 从完整URL中提取文件名
        String fileName;
        if (name.contains("/")) {
            fileName = name.substring(name.lastIndexOf("/") + 1);
        } else {
            fileName = name;
        }
        
        // 构建MinIO文件访问路径
        StringBuilder stringBuilder = new StringBuilder();
        if (!minioUtil.getEndpoint().startsWith("http")) {
            stringBuilder.append("https://");
        }
        stringBuilder
                .append(minioUtil.getEndpoint())
                .append("/")
                .append(minioUtil.getBucketName())
                .append("/")
                .append(fileName);
                
        return Result.success(stringBuilder.toString());
    }
}